<?php
/**
 * 生成缩略图
 *
 * @package     controller
 * @author      Akon(番茄红了) <aultoale@gmail.com>
 * @copyright   Copyright (c) 2008 (http://www.imeelee.com)
 * @license     http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class ThumbController extends Ext_Controller_Action
{

    /**
     * 初始化方法
     *
     */
    public function init()
    {
        $this->view = null;
    }

    /**
     * 集中处理 REQUEST_URI
     *
     * @param  string  $method
     * @param  array   $args
     */
    public function noRouter($method, $args)
    {
        $thumbLink = urldecode($this->request->getRequestUri());

        preg_match('/([^\.]+)\.\((\d+)(x\d+)?\)\.(\w+)/', $thumbLink, $matches);
        count($matches) != 5 && die('无效的缩略图文件格式');

        list($filename, $basename, $width, $height, $extension) = $matches;
        intval($width)  < 10 && die('无效的缩略图高度');
        intval($height) == 0 && $height = false;

        $sourceLink = str_ireplace(
            str_replace(EGP_WEB_DIR, '', $this->config->thumb->basePath). '/',
            str_replace(EGP_WEB_DIR, '', $this->config->upload->basePath) . '/',
            str_replace($filename, "$basename.$extension", $thumbLink)
        );
        $sourceFile = EGP_WEB_DIR . $sourceLink;

        !is_file($sourceFile) && die('图片源文件不存在');

        $thumb = new EGP_Image_Thumb($sourceFile);
        $thumb->make($width, $height)->output(true, EGP_WEB_DIR . $thumbLink);
    }

}